<?php
/**
 * @author yuanyitian
 * @date 2024/8/15 0:19
 */

namespace api\admin;

class package
{

    public function getList($request){
        date_default_timezone_set('Asia/Shanghai');
        $page=(int)$request['page'];
        $size=(int)$request['size'];
        $name=$request['name'];

        $start=($page-1)*$size;

        $con=con();
        $sql="select * from package where is_delete=1";
        if(!empty($name)){
            $sql.=" and name like '%$name%'";
        }
        $data=mysqli_query($con,$sql." order by `id` DESC limit $start,$size");
        $data=mysqli_fetch_all($data,true);
        $total=mysqli_query($con,$sql);
        $total=mysqli_num_rows($total);
        mysqli_close($con);

        for($i=0;$i<count($data);$i++){
            $data[$i]['price_first']=sprintf("%.2f",$data[$i]['price_first']/100);
            $data[$i]['price_1']=sprintf("%.2f",$data[$i]['price_1']/100);
            $data[$i]['price_2']=sprintf("%.2f",$data[$i]['price_2']/100);
            $data[$i]['price_3']=sprintf("%.2f",$data[$i]['price_3']/100);
            $data[$i]['date']=explode(',',$data[$i]['date']);
        }

        return out_right([
            'data'=>$data,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size)
        ]);
    }

    public function add($request){
        date_default_timezone_set('Asia/Shanghai');
        $today_time=strtotime(date('Y-m-d'));

        $name=$request['name'];
        $introduce=$request['introduce'];
        $price_first=$request['price_first']*100;
        $price_1=$request['price_1']*100;
        $price_2=$request['price_2']*100;
        $price_3=$request['price_3']*100;
        $date=implode(',',$request['date']);
        $remark=$request['remark'];
        $time=time();

        $con=con();
        $res=mysqli_query($con,"insert into package(name,introduce,date,price_first,price_1,price_2,price_3,remark,create_time,update_time) values('$name','$introduce','$date',$price_first,$price_1,$price_2,$price_3,'$remark',$time,$time)");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    public function edit($request){
        date_default_timezone_set('Asia/Shanghai');

        $id=(int)$request['id'];
        $name=$request['name'];
        $introduce=$request['introduce'];
        $price_first=$request['price_first']*100;
        $price_1=$request['price_1']*100;
        $price_2=$request['price_2']*100;
        $price_3=$request['price_3']*100;
        $date=implode(',',$request['date']);
        $remark=$request['remark'];
        $time=time();

        $con=con();
        $res=mysqli_query($con,"update package set name='$name',introduce='$introduce',price_first=$price_first,price_1=$price_1,price_2=$price_2,price_3=$price_3,remark='$remark',date='$date',update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error('修改失败或数据无变动！');
        }
    }

    public function del($request){
        $id=(int)$request['id'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update package set is_delete=0,delete_time=$time where id=$id");
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    public function changeStatus($request){
        $id=(int)$request['id'];
        $status=(int)$request['status'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update package set status=$status,update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    public function getGoods($request){
        $menu_id=(int)$request['menu_id'];
        $goods_name=$request['goods_name'];
        $page=(int)$request['page'];
        $size=(int)$request['size'];
        $start=($page-1)*$size;

        $con=con();
        $sql="select a.id,a.goods_id,b.`name`,b.first_price,b.price,b.vip_price,a.`status`,a.create_time,a.update_time from package_item a LEFT JOIN goods b on (a.goods_id=b.id)  where a.package_id=$menu_id  and a.is_delete=1 and b.is_delete=1 ";
        if(!empty($goods_name)){
            $where=" and b.name like '%$goods_name%'";
        }else{
            $where="";
        }
        $sql_0=$sql.$where;
        $select=mysqli_query($con,"select * from goods y where NOT FIND_IN_SET(y.id, (select IFNULL(GROUP_CONCAT(x.goods_id SEPARATOR ','),0) from ($sql) x))");
        $select=mysqli_fetch_all($select,true);
        $goods=mysqli_query($con,$sql_0." ORDER BY a.create_time limit $start,$size");
        $goods=mysqli_fetch_all($goods,true);
        $total=mysqli_query($con,$sql_0);
        $total=mysqli_num_rows($total);
        mysqli_close($con);
        for($i=0;$i<count($goods);$i++){
            $goods[$i]['create_time']=date('Y-m-d H:i:s',$goods[$i]['create_time']);
            if(!empty($goods[$i]['update_time'])){
                $goods[$i]['update_time']=date('Y-m-d H:i:s',$goods[$i]['update_time']);
            }
        }
        return out_right([
            'data'=>$goods,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size),
            'select'=>$select
        ]);
    }

    public function goodAdd($request){
        $menu_id=(int)$request['menu_id'];
        $goods_id=$request['goods_id'];
        $time=time();

        $con=con();
        for($i=0;$i<count($goods_id);$i++){
            $id=(int)$goods_id[$i];
            mysqli_query($con,"insert into package_item(package_id,goods_id,create_time) values($menu_id,$id,$time)");
        }
        mysqli_close($con);
        return out_right();
    }

    public function delGoods($request){
        $id=(int)$request['id'];
        $con=con();
        $res=mysqli_query($con,"update package_item set is_delete=0 where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error('删除失败或数据已删除！');
        }
    }

}